import { Component, Input } from '@angular/core';
import { CascaderItem } from 'ng-devui';
import { MaterialClass } from 'src/app/@core/data/material';
import { EncodeRulesService } from 'src/app/@core/services/encoderRule/encode-rules.service';
@Component({
  selector: 'app-edit-material-category',
  templateUrl: './edit-material-category.component.html',
  styleUrls: ['./edit-material-category.component.scss']
})
export class EditMaterialCategoryComponent {
  @Input() data: any;
  options :any;//部门
  MaterialCategory:MaterialClass={
    partClassName: '',
    partClassCode:'',
    parentPartClassId: null,
    note: '',
  };//分类类目
  constructor(){}
  ngOnInit(): void{
    this.options = this.convertToCascaderItem(this.data.options)
    this.MaterialCategory.note = this.data.option.note
    this.MaterialCategory.partClassName = this.data.option.partClassName
    this.MaterialCategory.partClassCode = this.data.option.partClassCode
    if(this.data.pathPids){
      this.MaterialCategory.parentPartClassId = this.data.pathPids[this.data.pathPids.length-1]
    }
  }
  convertToCascaderItem(data: any[]): CascaderItem[] {
    return data.map(item => {
      let cascaderItem: CascaderItem = {
        label: item.partClassName,
        value: item.partClassId,
      };
      if (item.subPartClasses) {
        cascaderItem.children = this.convertToCascaderItem(item.subPartClasses);
      }
      return cascaderItem;
    });
  }//转换数据格式为华为库练级菜单的数据格式
  onChanges($event: string | any[]){
    this.MaterialCategory.parentPartClassId = $event[$event.length-1]
  }
}
